package com.cal.utms.dao.maintenance;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.cal.utms.entity.FundManager;
import com.cal.utms.entity.FundManagerContactPerson;
import com.cal.utms.logAudit.AuditLogInterceptor;
import com.cal.utms.util.HibernateUtil;

public class FundManagerDaoImpl {

	public Object addFundManager(FundManager fundManager) {
		Object obj;
		AuditLogInterceptor interceptor = new AuditLogInterceptor();
		Session session = HibernateUtil.getSession().openSession(interceptor);
		interceptor.setSession(session);
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			obj = session.save(fundManager);
			tx.commit();
		} catch (Exception ex) {
			tx.rollback();
			obj = new Object();
		} finally {
			session.close();
		}
		return obj;
	}

	public FundManager getFundManagerById(Integer id) {
		Session session = HibernateUtil.getSession().openSession();
		FundManager fundManager;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			fundManager = (FundManager) session.get(FundManager.class, id);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			fundManager = new FundManager();
		} finally {
			session.close();
		}
		return fundManager;
	}

	@SuppressWarnings("unchecked")
	public List<FundManager> getFundManagerList() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<FundManager> fundManagers = null;
		try {
			tx = session.beginTransaction();
			fundManagers = (List<FundManager>) session.createQuery("FROM FundManager f").list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
		} finally {
			session.close();
		}
		return fundManagers;
	}

	public short addFundManagerContactPersons(List<FundManagerContactPerson> contactPersons) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		short result = 0;
		try {
			for (FundManagerContactPerson fundManagerContactPerson : contactPersons) {
				tx = session.beginTransaction();
				session.save(fundManagerContactPerson);
				result = 1;
				tx.commit();
			}
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
			result = 0;
		} finally {
			session.close();
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<FundManagerContactPerson> getFundManagerCntPersons(int fundManagerId) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<FundManagerContactPerson> contactPersons = null;
		try {
			tx = session.beginTransaction();
			contactPersons = session.createQuery("FROM FundManagerContactPerson f where f.fundManager.id=:id").setParameter("id", fundManagerId).list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return contactPersons;
	}

	public Object updateFundManager(FundManager fundManager) {
		Object obj = 0;
		AuditLogInterceptor interceptor = new AuditLogInterceptor();
		Session session = HibernateUtil.getSession().openSession(interceptor);
		interceptor.setSession(session);
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			obj = session.merge(fundManager);
			tx.commit();
		} catch (Exception ex) {
			if (tx != null)
				tx.rollback();
			ex.printStackTrace();
		} finally {
			session.close();
		}
		return obj;
	}

	public short addUpdateFundManagerContactPersons(List<FundManagerContactPerson> contactPersons, int fundManagerId) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		short result = 0;
		try {
			long count = (Long) session.createQuery("select count(*) from FundManagerContactPerson FC where FC.fundManager.id=:id").setParameter("id", fundManagerId)
					.uniqueResult();

			if (count > 0) {
				session.createQuery("DELETE FROM FundManagerContactPerson FC where FC.fundManager.id=:id").setParameter("id", fundManagerId).executeUpdate();
				for (FundManagerContactPerson fundManagerContactPerson : contactPersons) {
					tx = session.beginTransaction();
					session.save(fundManagerContactPerson);
					result = 1;
					tx.commit();
				}
			} else {
				for (FundManagerContactPerson fundManagerContactPerson : contactPersons) {
					tx = session.beginTransaction();
					session.save(fundManagerContactPerson);
					result = 1;
					tx.commit();
				}
			}
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
			result = 0;
		} finally {
			session.close();
		}
		return result;
	}
}
